package com.rs.service.impl.rs;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.BehavMapper;
import com.rs.dao.PerforscMapper;
import com.rs.dao.ProrateMapper;
import com.rs.dao.UserMapper;
import com.rs.model.CommonResult;
import com.rs.model.Behav;
import com.rs.model.Perforsc;
import com.rs.model.Task;
import com.rs.model.rs.User;
import com.rs.service.rs.BehavService;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class BehavServiceImpl implements BehavService {

	@Autowired
	private BehavMapper bem;
	@Autowired
	private ProrateMapper pm;
	@Autowired
	private PerforscMapper perm;

	@Autowired
	private UserMapper um;

	/*********APP**/
	/**
	 * 添加行为绩效
	 */
	@Override
	@Transactional
	public CommonResult<String> behavAddApp(List<Behav> list) {
		for (Behav behav : list) {
			//设置月份
			behav.setBeh_month(TimeTypeConversionUtil.getMonthDate());
			//设置上级评分状态
			behav.setBeh_type(0);
			bem.behavAddApp(behav);
		}
		return new CommonResult<String>(200, "添加成功");
	}

	/**
	 * 查询本人任务绩效（按月份）
	 */
	@Override
	public CommonResult<List<Behav>> behavQueryOwnApp(Behav behav) {
		List<Behav> list = bem.behavQueryOwnApp(behav);
		return new CommonResult<List<Behav>>(200, "查询成功", list);
	}

	/**
	 * 修改行为绩效
	 */
	@Override
	@Transactional
	public CommonResult<String> behavUpdateApp(List<Behav> list) {
		for (Behav behav : list) {
			bem.behavUpdateApp(behav);
		}
		return new CommonResult<String>(200, "修改成功");
	}

	/**
	 * 上级查看本部门的所有员工行为绩效
	 */
	@Override
	public CommonResult<List<Behav>> behavSuperiorsLookApp(Behav behav) {
		//初始化月份
		behav.setBeh_month(TimeTypeConversionUtil.conversionTime(behav.getBeh_month()));
		List<Behav> list = bem.behavSuperiorsLookApp(behav);
		return new CommonResult<List<Behav>>(200, "查询成功", list);
	}

	/**
	 * 上级评分
	 */
	@Override
	@Transactional
	public CommonResult<String> behavSuperiorsMarkApp(List<Behav> bList) {
		double score = 0.0;//上级评价总分
		for (Behav behav : bList) {
			behav.setBeh_type(1);//更改为已评价
			bem.behavSuperiorsMarkApp(behav);
			score = score + behav.getBeh_superiors();
		}

		//从数据库中取出任务比例
		double beh_ratio = Double.valueOf(pm.queryProrate().getPro_beh()) / 100;

		//计算业绩考核分数
		double behsc = score * beh_ratio;//任务绩效的总得分

		//beh_uid
		Integer beh_uid = bList.get(1).getBeh_uid();
		//月份
		Date beh_month = bList.get(1).getBeh_month();

		//查询数据库是否有该员工的绩效得分
		Perforsc p = perm.queryPerforscExists(beh_uid, beh_month);
		boolean flag = Optional.ofNullable(p).isPresent();
		if (flag) {//数据库有该员工的绩效得分，修改
			//pe_id
			Integer pe_id = p.getPe_id();
			//总得分
			double total = behsc + p.getPe_tasc();
			Perforsc perforsc = new Perforsc();
			perforsc.setPe_id(pe_id);
			perforsc.setPe_behsc(behsc);
			perforsc.setPe_totalsc(total);
			//修改绩效得分表
			perm.updatePerforsc(perforsc);
		} else {//数据库没有该员工的绩效得分，创建
			//将任务绩效分数设为0
			double total = behsc + 0.0;
			Perforsc perforsc = new Perforsc(beh_uid, 0.0, behsc, total, beh_month);
			//添加绩效得分
			perm.addPerforsc(perforsc);
		}
		return new CommonResult<String>(200, "评分成功");
	}

	/****************PC***************/
	/**
	 * pc端查询所有的行为绩效
	 */
	@Override
	public CommonResult<Map<String, Object>> behavQueryAllPc(Behav behav) {
		//将日期设为一号
		//behav.setBeh_month(TimeTypeConversionUtil.conversionTime(behav.getBeh_month()));

		Page<Task> page = PageHelper.startPage(behav.getPageNum(), 10);//分页
		List<Behav> list = bem.behavQueryAllPc();
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc端条件查询
	 */
	@Override
	public CommonResult<Map<String, Object>> behavConditionsQueryPc(Behav behav) {
		//将日期设为一号
		//behav.setBeh_month(TimeTypeConversionUtil.conversionTime(behav.getBeh_month()));

		Page<Task> page = PageHelper.startPage(behav.getPageNum(), 10);//分页
		List<Behav> list = bem.behavConditionsQueryPc(behav);
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc端查询行为绩效详情
	 */
	@Override
	public CommonResult<List<Behav>> behavCheckDetailsPc(Behav behav) {
		List<Behav> list = bem.behavCheckDetailsPc(behav);
		User user = um.userLookInfoApp(behav.getBeh_uid());
		for (Behav b : list) {
			//设置姓名
			b.setBeh_uname(user.getU_name());
			//设置部门
			b.setBeh_dename(user.getU_dename());
			//设置职位
			b.setBeh_poname(user.getU_poname());
		}

		return new CommonResult<List<Behav>>(200, "查询成功", list);
	}

}
